<?php
namespace zhtfrontend\components\services\statistics;

use common\components\helpers\Fun;
use yii\base\Component;
use zhtfrontend\models\datas\CheckCount;
use zhtfrontend\models\datas\CustomerRelationship;
use zhtfrontend\models\datas\Goods;
use zhtfrontend\models\datas\Mechanic;
use zhtfrontend\models\datas\MechanicSettlement;
use zhtfrontend\models\datas\OrdersItems;
use zhtfrontend\models\datas\ReportRecord;

/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/7/13
 * Time: 11:23
 */
class Statisticaldata extends Component
{
   public static function getAuditing($where,$stage){
       $list = MechanicSettlement::find()->select(['id'])->where($where)->all();
       $count = 0;
       foreach ($list as $item){
            $row = ReportRecord::find()->where(['module_id'=>$item->id,'type'=>2])->orderBy('id DESC')->one();
            $admin_id = \Yii::$app->user->id;
            if($row != null && $row->stage == $stage && $row->sendee_id == $admin_id && $row->status == 1){
                 $count++;
            }
       }
       return $count;
   }

   public static function getSubGoodsCount($where){
       return Goods::find()->where($where)->count();
   }

   public static function getSubSettlementCount(){
       $dates = CheckCount::find()->select(['FROM_UNIXTIME(class_time,"%Y-%m") as months'])->groupBy('months')->asArray()->all();
       $count = 0;
       $nowTime = time();
       $BeginDates= date("Y-m-1");
       $BeginDate = strtotime($BeginDates);
       $settlementTime = strtotime(date('y-m-21'));
       $EndDate = strtotime("$BeginDate +1 month");
       if($nowTime > $BeginDate  && $nowTime < $settlementTime){
             $compareTime = strtotime(date('Y-m-21', strtotime("$BeginDates -1 month")));
       }
       if($nowTime > $settlementTime && $nowTime < $EndDate){
           $compareTime = $settlementTime;
       }
       foreach ($dates as $item){
              $start = $item['months'].'-21';
              $startTime = strtotime("$start-1month");
              $endTime = strtotime($start);
              $c= CheckCount::find()->where(['status'=>0])->andWhere(['between','class_time',$startTime,$endTime])->andWhere(['<','class_time',$compareTime])->groupBy('mechanic_id')->count();
              $count +=$c;
       }
       return $count;
   }
    public static function getAuditGoodsCount($where,$stage){
        $goodsList = Goods::find()->where($where)->all();
        $count = 0;
        foreach ($goodsList as $item){
            $row = ReportRecord::find()->where(['module_id'=>$item->id,'type'=>1])->orderBy('id DESC')->one();
            if($row != null && $row->stage == $stage){
                $count++;
            }
        }
        return $count;
    }

    public static function getSubGoodsList($where,$url){
         $res = Goods::find()->select(['name','id'])->where($where)->all();
         $data = [];

         foreach ($res as $item){
             $row = [
                 'url'=>Fun::url([$url,'id'=>$item->id]),
                 'title' =>  isset($item->mechanic->name) ? $item->mechanic->name : ''.$item->name
             ];
             $data[] = $row;
         }
         return $data;
    }

    public static function getSubSettlementList(){
        $dates = CheckCount::find()->select(['FROM_UNIXTIME(class_time,"%Y-%m") as months'])->groupBy('months')->asArray()->all();
        $nowTime = time();
        $BeginDates= date("Y-m-1");
        $BeginDate = strtotime($BeginDates);
        $settlementTime = strtotime(date('y-m-21'));
        $EndDate = strtotime("$BeginDate +1 month");
        if($nowTime > $BeginDate  && $nowTime < $settlementTime){
            $compareTime = strtotime(date('Y-m-21', strtotime("$BeginDates -1 month")));
        }
        if($nowTime > $settlementTime && $nowTime < $EndDate){
            $compareTime = $settlementTime;
        }
        $data = [];
        foreach ($dates as $item){
            $start = $item['months'].'-21';
            $startTime = strtotime("$start-1month");
            $endTime = strtotime($start);
            $List= CheckCount::find()->select(['mechanic_id','SUM(settlement_price) as settlement'])->where(['status'=>0])->andWhere(['between','class_time',$startTime,$endTime])->andWhere(['<','class_time',$compareTime])->groupBy('mechanic_id')->asArray()->all();
            foreach ($List as $k=>$items){
                $row = [
                    'url' => Fun::url(['orderstatistic/checkmechanic','time_month'=>$item['months'],'mechanic_id'=>$items['mechanic_id']]),
                    'title'=>$item['months'].' '.(Mechanic::findOne($items['mechanic_id']))->name.''.' 结算金额 '.$items['settlement']
                ];
                $data[] = $row;
            }
        }
        return $data;
    }

    public static function getAuditSettlementList($where,$stage,$url){
        $list = MechanicSettlement::find()->select(['id','mechanic_id','commission_money','subsidy_money','additional_money'])->where($where)->asArray()->all();
        $count = 0;
        $data = [];
        foreach ($list as $item){
            $row = ReportRecord::find()->where(['module_id'=>$item['id'],'type'=>2])->orderBy('id DESC')->one();
            $admin_id = \Yii::$app->user->id;
            if($row != null && $row->stage == $stage && $row->sendee_id == $admin_id && $row->status == 1){
                $row = [
                    'url' => Fun::url($url,['id'=>$item['id']]),
                    'title'=>isset($item->mechanic->name) ? $item->mechanic->name:''.' 结算金额 '.$item['commission_money']+$item['subsidy_money']+$item['additional_money']
                ];
                $data[] = $row;
            }
        }
        return $data;
    }
    public static function getMechanicAuditing($where,$stage){
        $list = MechanicSettlement::find()->select(['id'])->where($where)->all();
        $count = 0;
        foreach ($list as $item){
            $row = ReportRecord::find()->where(['module_id'=>$item->id,'type'=>2])->orderBy('id DESC')->one();
            if($row != null && $row->stage == $stage){
                $count++;
            }
        }
        return $count;
    }
    public static function getSettlementList($where,$stage,$url){
        $list = MechanicSettlement::find()->select(['id','mechanic_id','commission_money','subsidy_money','additional_money'])->where($where)->asArray()->all();
        $count = 0;
        $data = [];
        foreach ($list as $item){
            $row = ReportRecord::find()->where(['module_id'=>$item->id,'type'=>2])->orderBy('id DESC')->one();
            if($row != null && $row->stage == $stage && $row->status == 1){
                $row = [
                    'url' => Fun::url($url,['id'=>$item['id']]),
                    'title'=>isset($item->mechanic->name) ? $item->mechanic->name:''.'结算金额 '.$item['commission_money']+$item['subsidy_money']+$item['additional_money']
                ];
                $data[] = $row;
            }
        }
        return $count;
    }

    // 统计教服负责机构好评率
    public static function getMechanicPraiseRate(){

    }

    //负责机构约课数量
    public static function getMechanicGoodsNum($where=[],$group = null){
        $mechanicIds = Mechanic::find()->select('id')->where($where)->column();
        $model = OrdersItems::find()->joinWith('orderss')->where(['{{%orders_items}}.mechanic_id'=>$mechanicIds,'teach_type'=>1]);
        if($group != null){
            $model->groupBy($group);
        }
        $count = $model->count();
        return $count;
    }

    public static function getRelationshipNum($where){
       return CustomerRelationship::find()->where($where)->count();
    }

}